$(function () {
    var insure_Form = $("#insure_Form")
    var client_dialog = $("#client_dialog")
    var client_datagrid = $("#client_datagrid")
    var car_dialog = $("#car_dialog")
    var car_datagrid = $("#car_datagrid")
    var product_dialog = $("#product_dialog")
    var product_datagrid = $("#product_datagrid")
    var productItem_datagrid = $("#productItem_datagrid")

    $("#submit").linkbutton({
        onClick: function () {
            insure_Form.form('submit', {
                url: '/insureInfo/saveOrUpdate.do',
                onSubmit: function (params) {
                    var rows = productItem_datagrid.datagrid('getRows')
                    $.each(rows, function (index, value) {
                        params["products[" + index + "].id"] = value.id
                        params["products[" + index + "].productName"] = value.productName
                        params["products[" + index + "].sn"] = value.sn
                        params["products[" + index + "].annualFee"] = value.annualFee
                    })
                   return insure_Form.form('validate')
                },
                success: function (data) {//成功之后的回调函数
                    data = $.parseJSON(data)
                    if (data.success) {
                        $.messager.alert('友情提示', '保存成功,请及时提醒管理员审核','info',function () {
                            window.location.reload()
                        });
                    } else {
                        $.messager.alert("友情提示", data.msg, "warning")
                    }
                }
            });

        }
    })
    $("#queryLinkbutton").linkbutton({
        onClick:function () {
            //获取高级查询的参数值
            var clientName = $("#clientName").textbox('getValue')
            //重新加载数据表格
            client_datagrid.datagrid('load', {
                keyword: clientName,
            });
        }
    })
    $("#client_linkbutton").linkbutton({
        iconCls: 'icon-search',
        onClick: function () {
            client_dialog.dialog('open')
        }
    })
    $("#car_linkbutton").linkbutton({
        iconCls: 'icon-search',
        onClick: function () {
            car_dialog.dialog('open')
        }
    })
    $("#add_linkbutton").linkbutton({
        iconCls: 'icon-add',
        onClick: function () {
            //判断有没有选中数据
            if ($("#organization_combogrid").val() == '请选择') {
                $.messager.alert("友情提示", "亲, 请先选择承保机构!", "info")
                return
            }
            product_dialog.dialog('open')
        }
    })
    client_dialog.dialog({
        closed: true,
        title: '选择客户',
        width: 700,
        height: 500,
        buttons: "#client_buttons"
    })
    productItem_datagrid.datagrid({
        fitColumns: true,
        toolbar: "#productItem_buttons",
        columns: [[
            {field: 'id', hidden: true},
            {field: 'productName', title: '保险产品', width: 100},
            {field: 'sn', title: '保险编号', width: 100},
            {field: 'annualFee', title: '保险年费', width: 100},
        ]],
    })
    client_datagrid.datagrid({
        url: '/client/query.do',
        pagination: true,
        fitColumns: true,
        rownumbers: true,
        singleSelect: true,
        fit: true,
        toolbar: "#client_toorbar",
        columns: [[
            {field: 'id', hidden: true},
            {field: 'name', title: '客户姓名', width: 100},
            {
                field: 'gender', title: '性别', width: 100, formatter: function (value) {
                    return value ? '男' : '女';
                }
            },
            {field: 'tel', title: '电话', width: 100, align: 'right'},
            {field: 'address', title: '客户地址', width: 100, align: 'right'},
            {field: 'identitynum', title: '身份证号', width: 100, align: 'right'}
        ]]

    })
    product_dialog.dialog({
        closed: true,
        title: '选择保险产品',
        width: 700,
        height: 500,
        buttons: "#product_buttons",
    })
    product_datagrid.datagrid({
        url: '/product/query.do',
        pagination: true,
        fitColumns: true,
        rownumbers: true,
        singleSelect: true,
        fit: true,
        columns: [[
            {field: 'id', hidden: true},
            {field: 'sn', title: '保险编号', width: 100},
            {field: 'productName', title: '保险名称', width: 100,},
            {
                field: 'organization', title: '保险机构', width: 100, formatter: function (value, row, index) {
                    return value.name;
                }
            },
            {field: 'period', title: '保障年限', width: 100, align: 'right'},
            {field: 'amount', title: '保额', width: 100, align: 'right'},
            {field: 'annualFee', title: '基本年费', width: 100, align: 'right'},
            {
                field: 'inSale', title: '销售状态', width: 100, align: 'right', formatter: function (value, row, index) {
                    if (value) {
                        return '<font color="blue">在售</font>'
                    } else {
                        return '<font color="red">停售</font>'
                    }
                }
            },
            {
                field: 'noCompensate',
                title: '不计免赔',
                width: 100,
                align: 'right',
                formatter: function (value) {
                    if (value) {
                        return '<font color="blue">是</font>'
                    } else {
                        return '<font color="red">否</font>'
                    }
                }
            }
        ]]

    })
    $("#organization_combogrid").combogrid({
        panelWidth: 450,
        idField: 'id',
        textField: 'name',
        url: '/organization/selectOrganizationInfo.do',
        columns: [[
            {field: 'id', title: '编号', width: 60},
            {field: 'sn', title: '机构代码', width: 100},
            {field: 'name', title: '机构名称', width: 120},
            {field: 'legalPerson', title: '法人代表', width: 100}
        ]],
        onSelect: function (index, row) {
            product_datagrid.datagrid('load', {
                organizationId: row.id,
                inSale:true
            })
            $("#organizationId").val(row.id)
            //清空明细表格
            productItem_datagrid.datagrid('loadData', [])
            //把总额设置为0
            $("#totalAmount").html(0)
            //把年限设置为空
            $("#burningTime").textbox('clear')
        }

    })
    $("#client_ok").linkbutton({
        onClick: function () {
            //判断是否选中行
            var row = client_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert("友情提示", "至少选中一行数据", "info")
                return;
            }
            $("#clientId").val(row.id)
            $("#insure_panel1 span[tag='name']").html(row.name)
            $("#insure_panel1 span[tag='gender']").html(row.gender ? '男' : '女')
            $("#insure_panel1 span[tag='tel']").html(row.tel)
            $("#insure_panel1 span[tag='address']").html(row.address)
            $("#insure_panel1 span[tag='identitynum']").html(row.identitynum)
            //显示table
            $("#insure").show()
            //关闭窗口
            client_dialog.dialog('close')
        }
    })
    $("#product_ok").linkbutton({
        onClick: function () {
            //判断是否选中行
            var row = product_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert("友情提示", "至少选中一行数据", "info")
                return;
            }
            //判断选中行在数据表格中是否存在
            //获取表格中的所有行
            var rows = productItem_datagrid.datagrid("getRows");
            var ids = $.map(rows, function (p) {
                return p.id
            })
            if ($.inArray(row.id, ids) == -1) {
                productItem_datagrid.datagrid("appendRow", row)
            }
            //关闭窗口
            product_dialog.dialog('close')
        }
    })
    car_dialog.dialog({
        closed: true,
        title: '选择车辆',
        width: 700,
        height: 500,
        buttons: "#car_buttons",
    })
    car_datagrid.datagrid({
        url: '/car/selectAll.do',
        fitColumns: true,
        rownumbers: true,
        pagination:true,
        singleSelect: true,
        fit: true,
        columns: [[
            {field: 'brand', title: '车辆品牌', width: 80},
            {field: 'productName', title: '车辆型号', width: 120},
            {field: 'displacement', title: '排气量', width: 100, align: 'right'},
            {field: 'evaluation', title: '市场估价', width: 100, align: 'right'},
            {field: 'type', title: '分类', width: 70, align: 'right'},
            {field: 'size', title: '车载大小', width: 100, align: 'right'},
            {field: 'info', title: '备注信息', width: 100, align: 'right'},
        ]]
    })
    $("#car_ok").linkbutton({
        onClick: function () {
            //判断是否选中行
            var row = car_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert("友情提示", "至少选中一行数据", "info")
                return;
            }
            $("#brand").textbox('setValue', row.brand)
            $("#model").textbox('setValue', row.productName)
            $("#exhaust").textbox('setValue', row.displacement)
            $("#price").textbox('setValue', row.evaluation)
            $("#carClass").combobox('setValue', row.type)
            $("#size").combobox('setValue', row.size)
            //关闭窗口
            car_dialog.dialog('close')
        }
    })
    $("#product_remove").linkbutton({
        onClick: function () {
            //获取数据表格中选中的行
            //判断是否选中行
            var row = productItem_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert("友情提示", "至少选中一行数据", "info")
                return;
            }
            //获得选中行的索引
            var index = productItem_datagrid.datagrid('getRowIndex', row);
            //删除指定索引的行
            productItem_datagrid.datagrid('deleteRow', index)
            //把总额设置为0
            $("#totalAmount").html(0)
            //把年限设置为空
            $("#burningTime").textbox('clear')

        }
    })
    //保险时长
    $("#burningTime").textbox({
        width: 60,
        required: true,
        onChange: function (newValue, oldValue) {
            //获取数据表格中的所有的行
            var rows = productItem_datagrid.datagrid('getRows');
            //获取所有的基础年费
            var totalAmount = 0;
            $.each(rows, function (i, val) {
                totalAmount += val.annualFee
                console.log(totalAmount);
            })
            totalAmount = totalAmount * newValue
            //把总价设置进span
            $("#totalAmount").html(totalAmount)
        }
    })
    //投保人文本框只读

})